package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.Projecttype;
import com.itheima.mapper.ProjecttypeMapper;
import com.itheima.query.ProjectTypePageQuery;
import com.itheima.result.PageResult;
import com.itheima.service.ProjecttypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author guanhuaer
 * @since 2024-09-05
 */
@Service
public class ProjecttypeServiceImpl extends ServiceImpl<ProjecttypeMapper, Projecttype> implements ProjecttypeService {
    @Autowired
    private ProjecttypeMapper projecttypeMapper;

    @Override
    public PageResult pageQuery(ProjectTypePageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
        Page<Projecttype> page = projecttypeMapper.page(pageQuery);
        return new PageResult(page.getTotal(), page.getResult());
    }
    @Override
    public void Delects(String string) {
        String[] strings = string.split(",");
        ArrayList<Integer> list = new ArrayList<>();
        for (String item : strings) {
            projecttypeMapper.Deldect(Integer.valueOf(item));
        }
    }

    @Override
    public boolean projectTypeAdds(String fileName,String creator) {
        String UPLOAD_PATH = "D:\\cad\\vue\\毕业论文\\guanhuaer\\roomhotel\\src\\main\\resources\\static\\img\\real";
        try {
            //创建工作簿对象
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(UPLOAD_PATH + File.separator + fileName));
            //获取工作簿下sheet的个数
            int sheetNum = xssfWorkbook.getNumberOfSheets();
            System.out.println("该excel文件中总共有：" + sheetNum + "个sheet");
            //遍历工作簿中的所有数据
            for (int i = 0; i < sheetNum; i++) {
                //读取第i个工作表
                System.out.println("读取第" + (i + 1) + "个sheet");
                XSSFSheet sheet = xssfWorkbook.getSheetAt(i);
                //获取最后一行的num，即总行数。此处从0开始
                int maxRow = sheet.getLastRowNum();
                for (int row = 1; row <= maxRow; row++) {
                    //获取最后单元格num，即总单元格数 ***注意：此处从1开始计数***
                    int maxRol = sheet.getRow(row).getLastCellNum();
                    if (sheet.getRow(row).getCell(1) != null) {
                        return false;
                    }
                    if (String.valueOf(sheet.getRow(row).getCell(0)) != "创新创业。。。") {
                        Projecttype projecttype = new Projecttype();
                        projecttype.setProjectType(String.valueOf(sheet.getRow(row).getCell(0)));
                        projecttype.setCreator(creator);
                        projecttype.setModifier(creator);
                        projecttype.setCreateTime(LocalDateTime.now());
                        projecttype.setUpdateTime(LocalDateTime.now());
                        projecttypeMapper.projectTypeAdd(projecttype);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 新增项目类型
     * @param projectType
     */
    @Override
    public void projectTypeAdd(Projecttype projectType) {
        projectType.setModifier(projectType.getCreator());
        projectType.setCreateTime(LocalDateTime.now());
        projectType.setUpdateTime(LocalDateTime.now());
        projecttypeMapper.projectTypeAdd(projectType);
    }
}
